package com.techm.smptest.network;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class ConnectServer {
	private String authpair, authentication, payload, response, url;
	private Base64 encoder = new Base64();

	public ConnectServer(String url, String payload, String username,
			String password) {
		this.url = url;
		this.payload = payload;
		authpair = username + ":" + password;
		authentication = new String(encoder.encode(authpair.getBytes()));
	}

	public String getResponse() {
		return response;
	}

	public void POST() {
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);

			StringEntity se = new StringEntity(payload, "UTF-8");
			se.setContentType("text/xml");
			// setting HTTP headers...
			post.setHeader("Content-Type", "application/xml");
			post.setHeader("Authorization", "Basic " + authentication);
			post.setHeader("X-SMP-APPCID", "");
			post.setEntity(se);

			Log.v("REQUEST", payload);
			HttpResponse httpResponse = httpclient.execute(post,
					new BasicHttpContext());

			StatusLine statusLine = httpResponse.getStatusLine();
			Log.v("HTTP Code " + statusLine.getStatusCode(),
					statusLine.getReasonPhrase());

			switch (statusLine.getStatusCode()) {
			case HttpStatus.SC_OK:
				HttpEntity entity = httpResponse.getEntity();
				if (entity != null) {
					response = EntityUtils.toString(entity);
					Log.v("RESPONSE", response);
				}
				break;
			case HttpStatus.SC_CREATED:

				break;
			default:
				Log.v("HTTP", "STATUS Error " + statusLine.getStatusCode());
				httpResponse.getEntity().getContent().close();
				throw new IOException(statusLine.getReasonPhrase());
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		} catch (IOException e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		}
	}

	public void GET() {
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);

			HttpResponse httpResponse = httpclient.execute(get,
					new BasicHttpContext());

			StatusLine statusLine = httpResponse.getStatusLine();
			Log.v("HTTP Code " + statusLine.getStatusCode(),
					statusLine.getReasonPhrase());

			switch (statusLine.getStatusCode()) {
			case HttpStatus.SC_OK:
				HttpEntity entity = httpResponse.getEntity();
				if (entity != null) {
					response = EntityUtils.toString(entity);
					Log.v("RESPONSE", response);
				}
				break;
			case HttpStatus.SC_CREATED:

				break;
			default:
				httpResponse.getEntity().getContent().close();
				throw new IOException(statusLine.getReasonPhrase());
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		} catch (IOException e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("RESPONSE EXCEPTION", e.toString());
		}
	}
}
